// OSR b ServerRecv
package org.jvcs.fmwk;

import org.jvcs.fmwk.*;
import org.jvcs.common.*;
import org.jvcs.utils.*;

import java.net.*;
import java.io.*;

public class ServerRecv extends Thread {
	public static int portNo = 0;
	ServerRecv(int portNo) {
		this.portNo = portNo;
	}
    public void run() {
		try {
				ServerSocket ss = new ServerSocket(portNo);
				Socket s2 = ss.accept();
				try {
								long fileTime = System.currentTimeMillis();
								File f=new File("c:/temp/server-recv1-"+fileTime+".txt");
								OutputStream outFile =new FileOutputStream(f);
								InputStream clientInputStream = null;
                                long startTime = System.currentTimeMillis();
                                byte[] buffer = new byte[Global.BUFFER_SIZE];
                                int read;
                                int totalRead = 0;
                                clientInputStream = s2.getInputStream();
                                while ((read = clientInputStream.read(buffer)) != -1) {
                                        totalRead += read;
										 outFile.write(buffer,0,read);
                                }
								clientInputStream.close();
								s2.close();
								outFile.close();
                                long endTime = System.currentTimeMillis();
                                System.out.println(totalRead + " bytes read in " + (endTime - startTime) + " ms.");
                        } catch (Exception e) {
							System.out.println("Error: Client File Transfer: "+e.toString());
                        }
				
			} catch (Exception e) {
				
				osr.pl("Issue in FileTransfer");
				
			}
		/*
		try {
			String java_cmd = "java -cp ";
			String jvcs_bin = System.getenv(G.JVCS_BIN);
			String jvcs_sa_program = "ServerFileTransfer";
			String filePath = "c:/temp/TEST-SERVER.txt";
			String command = java_cmd + jvcs_bin + " " + jvcs_sa_program + " "+ portNo + " "+filePath;
			Process child = Runtime.getRuntime().exec(command);
			child.waitFor();
			System.out.println(child.exitValue());


		} catch (Exception e) {
			osr.pl("Error in the run of FileTransfer Server"+e.toString());
		}
		*/
    }
}
